// Package _6__合并区间 main
// @Title _6__合并区间
// @Description
// @Author  == 琳琳也叫王木木 ==  2022-03-02 18:51
// @Create  2022-03-02 18:51
package main

import (
	"fmt"
	"sort"
)

func merge(intervals [][]int) [][]int {
	var ans [][]int

	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i][0] < intervals[j][0]
	})
	prev := intervals[0]
	for i := 1; i < len(intervals); i++ {
		cur := intervals[i]
		if prev[1] < cur[0] {
			// 没有重合
			ans = append(ans, prev)
			prev = cur
		} else {
			prev[1] = max(prev[1], cur[1])
		}
	}
	ans = append(ans, prev)
	return ans
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	nums := [][]int{
		{1, 3},
		{15, 18},
		{8, 10},
		{2, 4},
	}
	fmt.Println(merge(nums))
}
